package com.itheima.reggie.filter;

/**
 * @Author lvFF
 * @Date 2022/04/25/下午 5:04;
 * Description
 */

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器，检查用户是否完成登入
 */
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    //专业用于路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        log.info("过滤器执行.....{}", request.getRequestURI());
        //获取本次请求的URI
        String requestURI = request.getRequestURI();
        //创建哪些路径需要过滤 哪些不过滤
        //不需要处理的路径
        String[] urls = {
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/**",
                "/user/sendMsg",//移动端发送短信
                "/user/login"//移动端发送短信
        };
        //根据路径判断是否需要过滤
        boolean check = check(urls, requestURI);
        if (check) {
            log.info("路径匹配");
            filterChain.doFilter(request, response);
            return;
        }
        //判断登入状态 是否过滤
        //不为空 代表登入 放行
        if (request.getSession().getAttribute("Employee") != null) {
            log.info("登入状态");
            Long empId =(Long) request.getSession().getAttribute("Employee");
            BaseContext.setCurrentId(empId);
            filterChain.doFilter(request, response);
            return;
        }
        //判断登入状态 是否过滤
        //不为空 代表登入 放行
        if (request.getSession().getAttribute("user") != null) {
            log.info("登入状态");
            Long userId =(Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);
            filterChain.doFilter(request, response);
            return;
        }
        // if (res.data.code === 0 && res.data.msg === 'NOTLOGIN') {// 返回登录页面
        //未登入
        //通过输出流的方式响应数据
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        log.info("未登入");
        return;
        // filterChain.doFilter(request,response);
    }

    /**
     * @param urls
     * @param url
     * @return
     */
    public boolean check(String[] urls, String url) {
        //便利urls路径
        for (String s : urls) {
            //比较 路径 是否相同  相同就返回
            //s 与url 顺序问题
            boolean match = PATH_MATCHER.match(s, url);
            if (match) {
                return true;
            }

        }
        return false;
    }
}
